/*
 * File:   FinCourseGauche_WS.c
 * Author: aravelomanantsoa@gmail.com
 */

#include <stdio.h>
#include "CAN_I.h"
#include "Utils.h"
#include "cJSON.h"

/* print JSON response */
void printJSONResponse(BYTE fin);

/* Main function */
int main(void) {
	int success = 0;
	BYTE fin = 0;

	printf(CONTENT_TYPE_JSON);

	success = fin_de_course(FIN_DE_COURSE_GAUCHE, &fin);

	if (success == CANNOT_OPEN_CAN_DEVICE) {
		printJSONErrorMsg("Cannot open CAN device!");
		return 0;
	}

	if (success == CANNOT_READ_CAN_MSG) {
		printJSONErrorMsg("Cannot read CAN message!");
		return 0;
	}

	printJSONResponse(fin);

	return 0;
}

void printJSONResponse(BYTE fin) {
	cJSON *root = cJSON_CreateObject();
	/* success = TRUE */
	cJSON_AddTrueToObject(root, "success");

	cJSON_AddNumberToObject(root, "fin_de_course", fin);

	printf(cJSON_Print(root));
}
